#include "D_LOG.h"

void splicString(char *str, int len, int args, ...);

typedef struct{
    int id;
    int size;
    char data[65000];
    int state;
    int end;
} TransferBuffer;

enum MMSGSSATE{
    MMSG_STATE_OK = 0,
    MMSG_STATE_FAILD = 1
};

struct MMSG{
    int  source_id;
    int  target_id;
    char content[1024];
    int  size;
};

class MMSG_OP{
    private:
        struct MMSG mMsg;
        int msgState;
        int mHandle;
    public:
        MMSG_OP(int src, int dst, int handle){
            mMsg.source_id = src;
            mMsg.target_id = dst;
            mHandle = handle;
        }
        void Send(const char *content){
            //MY_LOGD("%s", content);
            strcpy(mMsg.content, content);
            int sendRes = send(mHandle, &mMsg, sizeof(struct MMSG), 0);
            //MY_LOGD("%d", sendRes);

            recv(mHandle, &msgState, sizeof(msgState), 0);
        }

        void Recv(){
            recv(mHandle, &mMsg, sizeof(struct MMSG), 0);
        }

        int GetState(){
            return msgState;
        }

};


typedef struct {
    int sockfd;
    int conn;
}AppConn;


class NetHandle {
    private:
        int  mPort;
        char mIp[15];

        int mSockfd;
        int mConn;

        int mHandle;

    public:
        NetHandle(const char *ip_str, int port = 12106) : mPort(port){
            strcpy(mIp, ip_str);
            mPort = port;
            MY_LOGD("You Server Info %s:%d", mIp, mPort);

            ConnectServer();
        };
        void ConnectServer() {

            mSockfd = socket(AF_INET, SOCK_STREAM, 0);

            ///定义sockaddr_in
            struct sockaddr_in servaddr;
            memset(&servaddr, 0, sizeof(servaddr));

            servaddr.sin_family = AF_INET;
            servaddr.sin_port = htons(mPort);
            servaddr.sin_addr.s_addr = inet_addr(mIp);

            ///连接服务器, 成功返回0, 错误返回-1
            if (connect(mSockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
            {
                perror("connect");
                exit(1);
            }

        }

        int getServerHandle(){
            return mSockfd;
        }

        int operator*() const{
            return mSockfd;
        }
};


typedef struct{
    int id[100];
    char *name[100];
} USER_LIST;
